package com.leyou.item.web;

import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("spec")
public class SpecificationController {

    @Autowired
    private SpecificationService specService;
    @GetMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupByCid(@PathVariable("cid") Long cid){

        return ResponseEntity.ok(specService.queryGroupByCid(cid));
    }

    @PostMapping("groups")
    public ResponseEntity<Void> saveGroup(@RequestParam("cid") Long cid, @RequestParam("name") String name){
        specService.saveGroup(cid, name);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @DeleteMapping("groups/{id}")
    public ResponseEntity<Void> deleteGroupById(@PathVariable("id") Long id){
        specService.deleteGroupById(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("params")
    public  ResponseEntity<List<SpecParam>> queryParamByGid(
            @RequestParam(value = "gid", required = false) Long gid,
            @RequestParam(value = "cid",required = false) Long cid,
            @RequestParam(value = "searching", required = false) Boolean searching){
        return ResponseEntity.ok(specService.queryParamByGid(gid, cid, searching));
    }

    /**
     * 根据分类id查询规格参数组
     * @param cid
     * @return
     */
    @GetMapping("group")
    public  ResponseEntity<List<SpecGroup>> queryListByCid(@RequestParam("cid") Long cid){
        return ResponseEntity.ok(specService.queryListByCid(cid));
    }
}
